home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / pcl-rev4.lha / std-class.lisp < prev    next >
Lisp/Scheme  |  1990-12-05  |  34KB  |  978 lines

  1. ;;;-*-Mode:LISP; Package:PCL; Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27.  
  28. (in-package 'pcl)
  29.  
  30. (define-gf-predicate classp class)
  31. (define-gf-predicate standard-class-p standard-class)
  32. (define-gf-predicate forward-referenced-class-p forward-referenced-class)
  33.  
  34.  
  35.  
  36. (defmethod shared-initialize :after ((object documentation-mixin)
  37.                      slot-names
  38.                      &key documentation)
  39.   (declare (ignore slot-names))
  40.   (setf (plist-value object 'documentation) documentation))
  41.  
  42.  
  43. (defmethod documentation (object &optional doc-type)
  44.   (lisp:documentation object doc-type))
  45.  
  46. (defmethod (setf documentation) (new-value object &optional doc-type)
  47.   (declare (ignore new-value doc-type))
  48.   (error "Can't change the documentation of ~S." object))
  49.  
  50.  
  51. (defmethod documentation ((object documentation-mixin) &optional doc-type)
  52.   (declare (ignore doc-type))
  53.   (plist-value object 'documentation))
  54.  
  55. (defmethod (setf documentation) (new-value (object documentation-mixin) &optional doc-type)
  56.   (declare (ignore doc-type))
  57.   (setf (plist-value object 'documentation) new-value))
  58.  
  59.  
  60. (defmethod documentation ((slotd standard-slot-definition) &optional doc-type)
  61.   (declare (ignore doc-type))
  62.   (slot-value slotd 'documentation))
  63.  
  64. (defmethod (setf documentation) (new-value (slotd standard-slot-definition) &optional doc-type)
  65.   (declare (ignore doc-type))
  66.   (setf (slot-value slotd 'documentation) new-value))
  67.  
  68.  
  69. ;;;
  70. ;;; Various class accessors that are a little more complicated than can be
  71. ;;; done with automatically generated reader methods.
  72. ;;;
  73. (defmethod class-wrapper ((class pcl-class))
  74.   (with-slots (wrapper) class
  75.     (let ((w?  wrapper))
  76.       (if (consp w?)
  77.       (let ((new (make-wrapper class)))
  78.         (setf (wrapper-instance-slots-layout new) (car w?)
  79.           (wrapper-class-slots new) (cdr w?))
  80.         (setq wrapper new))
  81.       w?))))
  82.  
  83. (defmethod class-precedence-list ((class pcl-class))
  84.   (unless (class-finalized-p class) (finalize-inheritance class))
  85.   (with-slots (class-precedence-list) class class-precedence-list))
  86.  
  87. (defmethod class-finalized-p ((class pcl-class))
  88.   (with-slots (wrapper) class (not (null wrapper))))
  89.  
  90. (defmethod class-prototype ((class std-class))
  91.   (with-slots (prototype) class
  92.     (or prototype (setq prototype (allocate-instance class)))))
  93.  
  94. (defmethod class-direct-default-initargs ((class std-class))
  95.   (plist-value class 'direct-default-initargs))
  96.  
  97. (defmethod class-default-initargs ((class std-class))
  98.   (plist-value class 'default-initargs))
  99.  
  100. (defmethod class-constructors ((class std-class))
  101.   (plist-value class 'constructors))
  102.  
  103. (defmethod class-slot-cells ((class std-class))
  104.   (plist-value class 'class-slot-cells))
  105.  
  106.  
  107. ;;;
  108. ;;; Class accessors that are even a little bit more complicated than those
  109. ;;; above.  These have a protocol for updating them, we must implement that
  110. ;;; protocol.
  111. ;;; 
  112.  
  113. ;;;
  114. ;;; Maintaining the direct subclasses backpointers.  The update methods are
  115. ;;; here, the values are read by an automatically generated reader method.
  116. ;;; 
  117. (defmethod add-direct-subclass ((class class) (subclass class))
  118.   (with-slots (direct-subclasses) class
  119.     (pushnew subclass direct-subclasses)
  120.     subclass))
  121.  
  122. (defmethod remove-direct-subclass ((class class) (subclass class))
  123.   (with-slots (direct-subclasses) class
  124.     (setq direct-subclasses (remove subclass direct-subclasses))
  125.     subclass))
  126.  
  127. ;;;
  128. ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
  129. ;;;
  130. ;;; There are four generic functions involved, each has one method for the
  131. ;;; class case and another method for the damned EQL specializers. All of
  132. ;;; these are specified methods and appear in their specified place in the
  133. ;;; class graph.
  134. ;;;
  135. ;;;   ADD-METHOD-ON-SPECIALIZER
  136. ;;;   REMOVE-METHOD-ON-SPECIALIZER
  137. ;;;   SPECIALIZER-METHODS
  138. ;;;   SPECIALIZER-GENERIC-FUNCTIONS
  139. ;;;
  140. ;;; In each case, we maintain one value which is a cons.  The car is the list
  141. ;;; methods.  The cdr is a list of the generic functions.  The cdr is always
  142. ;;; computed lazily.
  143. ;;;
  144.  
  145. (defmethod add-method-on-specializer ((method method) (specializer class))
  146.   (with-slots (direct-methods) specializer
  147.     (setf (car direct-methods) (adjoin method (car direct-methods))    ;PUSH
  148.       (cdr direct-methods) ()))
  149.   method)
  150.  
  151. (defmethod remove-method-on-specializer ((method method) (specializer class))
  152.   (with-slots (direct-methods) specializer
  153.     (setf (car direct-methods) (remove method (car direct-methods))
  154.       (cdr direct-methods) ()))
  155.   method)
  156.  
  157. (defmethod specializer-methods ((specializer class))
  158.   (with-slots (direct-methods) specializer
  159.     (car direct-methods)))
  160.  
  161. (defmethod specializer-generic-functions ((specializer class))
  162.   (with-slots (direct-methods) specializer
  163.     (or (cdr direct-methods)
  164.     (setf (cdr direct-methods)
  165.           (gathering1 (collecting-once)
  166.         (dolist (m (car direct-methods))
  167.           (gather1 (method-generic-function m))))))))
  168.  
  169.  
  170.  
  171. ;;;
  172. ;;; This hash table is used to store the direct methods and direct generic
  173. ;;; functions of EQL specializers.  Each value in the table is the cons.
  174. ;;; 
  175. (defvar *eql-specializer-methods* (make-hash-table :test #'eql))
  176.  
  177. (defmethod add-method-on-specializer ((method method) (specializer eql-specializer))
  178.   (let* ((object (eql-specializer-object specializer))
  179.      (entry (gethash object *eql-specializer-methods*)))
  180.     (unless entry
  181.       (setq entry
  182.         (setf (gethash object *eql-specializer-methods*)
  183.           (cons nil nil))))
  184.     (setf (car entry) (adjoin method (car entry))
  185.       (cdr entry) ())
  186.     method))
  187.  
  188. (defmethod remove-method-on-specializer ((method method) (specializer eql-specializer))
  189.   (let* ((object (eql-specializer-object specializer))
  190.      (entry (gethash object *eql-specializer-methods*)))
  191.     (when entry
  192.       (setf (car entry) (remove method (car entry))
  193.         (cdr entry) ()))
  194.     method))
  195.  
  196. (defmethod specializer-methods ((specializer eql-specializer))  
  197.   (car (gethash (eql-specializer-object specializer) *eql-specializer-methods*)))
  198.  
  199. (defmethod specializer-generic-functions ((specializer eql-specializer))
  200.   (let* ((object (eql-specializer-object specializer))
  201.      (entry (gethash object *eql-specializer-methods*)))
  202.     (when entry
  203.       (or (cdr entry)
  204.       (setf (cdr entry)
  205.         (gathering1 (collecting-once)
  206.           (dolist (m (car entry))
  207.             (gather1 (method-generic-function m)))))))))
  208.  
  209.  
  210.  
  211. (defun real-load-defclass (name metaclass-name supers slots other accessors)
  212.   (do-standard-defsetfs-for-defclass accessors)                    ;***
  213.   (apply #'ensure-class name :metaclass metaclass-name
  214.                  :direct-superclasses supers
  215.                  :direct-slots slots
  216.                  :definition-source `((defclass ,name)
  217.                           ,(load-truename))
  218.                  other))
  219.  
  220. (defun ensure-class (name &rest all)
  221.   (apply #'ensure-class-using-class name (find-class name nil) all))
  222.  
  223. (defmethod ensure-class-using-class (name (class null) &rest args &key)
  224.   (multiple-value-bind (meta initargs)
  225.       (ensure-class-values class args)
  226.     (setf class (apply #'make-instance meta :name name initargs)
  227.       (find-class name) class)
  228.     (inform-type-system-about-class class name)                    ;***
  229.     class))
  230.  
  231. (defmethod ensure-class-using-class (name (class pcl-class) &rest args &key)
  232.   (multiple-value-bind (meta initargs)
  233.       (ensure-class-values class args)
  234.     (unless (eq (class-of class) meta) (change-class class meta))
  235.     (apply #'reinitialize-instance class initargs)
  236.     (inform-type-system-about-class class name)                    ;***
  237.     class))
  238.  
  239. (defun ensure-class-values (class args)
  240.   (let* ((initargs (copy-list args))
  241.      (unsupplied (list 1))
  242.      (supplied-meta   (getf initargs :metaclass unsupplied))
  243.      (supplied-supers (getf initargs :direct-superclasses unsupplied))
  244.      (supplied-slots  (getf initargs :direct-slots unsupplied))
  245.      (meta
  246.        (cond ((neq supplied-meta unsupplied)
  247.           (find-class supplied-meta))
  248.          ((or (null class)
  249.               (forward-referenced-class-p class))
  250.           *the-class-standard-class*)
  251.          (t
  252.           (class-of class))))
  253.      (proto (class-prototype meta)))  
  254.     (flet ((fix-super (s)
  255.          (cond ((classp s) s)
  256.            ((not (legal-class-name-p s))
  257.             (error "~S is not a class or a legal class name." s))
  258.            (t
  259.             (or (find-class s nil)
  260.             (setf (find-class s)
  261.                   (make-instance 'forward-referenced-class
  262.                          :name s)))))))      
  263.       (loop (unless (remf initargs :metaclass) (return)))
  264.       (loop (unless (remf initargs :direct-superclasses) (return)))
  265.       (loop (unless (remf initargs :direct-slots) (return)))
  266.       (values meta
  267.           (list* :direct-superclasses
  268.              (and (neq supplied-supers unsupplied)
  269.               (mapcar #'fix-super supplied-supers))
  270.              :direct-slots
  271.              (and (neq supplied-slots unsupplied) supplied-slots)
  272.              initargs)))))
  273.  
  274.  
  275. ;;;
  276. ;;;
  277. ;;;
  278. (defmethod shared-initialize :before ((class std-class)
  279.                       slot-names
  280.                       &key direct-superclasses)
  281.   (declare (ignore slot-names))
  282.   ;; *** error checking
  283.   )
  284.   
  285. (defmethod shared-initialize :after
  286.        ((class std-class)
  287.         slot-names
  288.         &key (direct-superclasses nil direct-superclasses-p)
  289.          (direct-slots nil direct-slots-p)
  290.          (direct-default-initargs nil direct-default-initargs-p))
  291.   (declare (ignore slot-names))
  292.   (setq direct-superclasses
  293.     (if direct-superclasses-p
  294.         (setf (slot-value class 'direct-superclasses)
  295.           (or direct-superclasses
  296.               (list *the-class-standard-object*)))
  297.         (slot-value class 'direct-superclasses)))
  298.   (setq direct-slots
  299.     (if direct-slots-p
  300.         (setf (slot-value class 'direct-slots)
  301.           (mapcar #'(lambda (pl) (make-direct-slotd class pl)) direct-slots))
  302.         (slot-value class 'direct-slots)))
  303.   (if direct-default-initargs-p
  304.       (setf (plist-value class 'direct-default-initargs) direct-default-initargs)
  305.       (setq direct-default-initargs (plist-value class 'direct-default-initargs)))
  306.   (setf (plist-value class 'class-slot-cells)
  307.     (gathering1 (collecting)
  308.       (dolist (dslotd direct-slots)
  309.         (when (eq (slotd-allocation dslotd) class)
  310.           (let ((initfunction (slotd-initfunction dslotd)))
  311.         (gather1 (cons (slotd-name dslotd)
  312.                    (if initfunction (funcall initfunction) *slot-unbound*))))))))
  313.   (add-direct-subclasses class direct-superclasses)
  314.   (add-slot-accessors    class direct-slots))
  315.  
  316. (defmethod reinitialize-instance :before ((class std-class)
  317.                       &key direct-superclasses
  318.                            direct-slots
  319.                            direct-default-initargs)
  320.   (declare (ignore direct-default-initargs))
  321.   (remove-direct-subclasses class (class-direct-superclasses class))
  322.   (remove-slot-accessors    class (class-direct-slots class)))
  323.  
  324. (defmethod reinitialize-instance :after ((class std-class)
  325.                      &rest initargs
  326.                      &key)
  327.   (update-class class nil)
  328.   (map-dependents class
  329.           #'(lambda (dependent)
  330.               (apply #'update-dependent class dependent initargs))))
  331.  
  332. (defun add-slot-accessors (class dslotds)
  333.   (fix-slot-accessors class dslotds 'add))
  334.  
  335. (defun remove-slot-accessors (class dslotds)
  336.   (fix-slot-accessors class dslotds 'remove))
  337.  
  338. (defun fix-slot-accessors (class dslotds add/remove)  
  339.   (flet ((fix (gfspec name r/w)
  340.        (let ((gf (ensure-generic-function gfspec)))
  341.          (case r/w
  342.            (r (if (eq add/remove 'add)
  343.               (add-reader-method class gf name)
  344.               (remove-reader-method class gf)))
  345.            (w (if (eq add/remove 'add)
  346.               (add-writer-method class gf name)
  347.               (remove-writer-method class gf)))))))
  348.     (dolist (dslotd dslotds)
  349.       (let ((slot-name (slotd-name dslotd)))
  350.     (dolist (r (slotd-readers dslotd)) (fix r slot-name 'r))
  351.     (dolist (w (slotd-writers dslotd)) (fix w slot-name 'w))))))
  352.  
  353.  
  354. (defun add-direct-subclasses (class new)
  355.   (dolist (n new)
  356.     (unless (memq class (class-direct-subclasses class))
  357.       (add-direct-subclass n class))))
  358.  
  359. (defun remove-direct-subclasses (class new)
  360.   (let ((old (class-direct-superclasses class)))
  361.     (dolist (o (set-difference old new))
  362.       (remove-direct-subclass o class))))
  363.  
  364.  
  365. ;;;
  366. ;;;
  367. ;;;
  368. (defmethod finalize-inheritance ((class std-class))
  369.   (update-class class t))
  370.  
  371.  
  372. ;;;
  373. ;;; Called by :after reinitialize instance whenever a class is reinitialized.
  374. ;;; The class may or may not be finalized.
  375. ;;; 
  376. (defun update-class (class finalizep)  
  377.   (when (or finalizep (class-finalized-p class))
  378.     (let* ((dsupers (class-direct-superclasses class))
  379.        (dslotds (class-direct-slots class))
  380.        (dinits  (class-direct-default-initargs class))
  381.        (cpl (compute-class-precedence-list class dsupers))
  382.        (eslotds (compute-slots class cpl dslotds))
  383.        (inits (compute-default-initargs class cpl dinits)))
  384.  
  385.       (update-cpl class cpl)
  386.       (update-slots class cpl eslotds)
  387.       (update-inits class inits)
  388.       (update-constructors class)))
  389.   (unless finalizep
  390.     (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
  391.  
  392. (defun update-cpl (class cpl)
  393.   (when (class-finalized-p class)
  394.     (unless (equal (class-precedence-list class) cpl)
  395.       (force-cache-flushes class)))
  396.   (setf (slot-value class 'class-precedence-list) cpl))
  397.  
  398. (defun update-slots (class cpl eslotds)
  399.   (multiple-value-bind (nlayout nwrapper-class-slots)
  400.       (compute-storage-info cpl eslotds)
  401.     ;;
  402.     ;; If there is a change in the shape of the instances then the
  403.     ;; old class is now obsolete.
  404.     ;;
  405.     (let* ((owrapper (class-wrapper class))
  406.        (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
  407.        (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
  408.        (nwrapper
  409.          (cond ((null owrapper)
  410.             (make-wrapper class))
  411.            ((and (equal nlayout olayout)
  412.              (not
  413.                (iterate ((o (list-elements owrapper-class-slots))
  414.                      (n (list-elements nwrapper-class-slots)))
  415.                  (unless (eq (car o) (car n)) (return t)))))
  416.             owrapper)
  417.            (t
  418.             ;;
  419.             ;; This will initialize the new wrapper to have the same
  420.             ;; state as the old wrapper.  We will then have to change
  421.             ;; that.  This may seem like wasted work (it is), but the
  422.             ;; spec requires that we call make-instances-obsolete.
  423.             ;;
  424.             (make-instances-obsolete class)
  425.             (class-wrapper class)))))
  426.       (with-slots (wrapper no-of-instance-slots slots) class
  427.     (setf no-of-instance-slots (length nlayout)
  428.           slots eslotds
  429.           (wrapper-instance-slots-layout nwrapper) nlayout
  430.           (wrapper-class-slots nwrapper) nwrapper-class-slots
  431.           wrapper nwrapper))
  432.       (dolist (eslotd eslotds)
  433.     (setf (slotd-class eslotd) class)
  434.     (setf (slotd-instance-index eslotd)
  435.           (instance-slot-index nwrapper (slotd-name eslotd)))))))
  436.  
  437. (defun compute-storage-info (cpl eslotds)
  438.   (let ((instance ())
  439.     (class    ()))
  440.     (dolist (eslotd eslotds)
  441.       (let ((alloc (slotd-allocation eslotd)))
  442.     (cond ((eq alloc :instance) (push eslotd instance))
  443.           ((classp alloc)       (push eslotd class)))))
  444.     (values (compute-layout cpl instance)
  445.         (compute-class-slots class))))
  446.  
  447. (defun compute-layout (cpl instance-eslotds)
  448.   (let* ((names
  449.        (gathering1 (collecting)
  450.          (dolist (eslotd instance-eslotds)
  451.            (when (eq (slotd-allocation eslotd) :instance)
  452.          (gather1 (slotd-name eslotd))))))
  453.      (order ()))
  454.     (labels ((rwalk (tail)
  455.            (when tail
  456.          (rwalk (cdr tail))
  457.          (dolist (ss (class-slots (car tail)))
  458.            (let ((n (slotd-name ss)))
  459.              (when (memq n names)
  460.                (setq order (cons n order)
  461.                  names (remove n names))))))))
  462.       (rwalk cpl)
  463.       (reverse (append names order)))))
  464.  
  465. (defun compute-class-slots (eslotds)
  466.   (gathering1 (collecting)
  467.     (dolist (eslotd eslotds)
  468.       (gather1
  469.     (assoc (slotd-name eslotd)
  470.            (class-slot-cells (slotd-allocation eslotd)))))))
  471.  
  472. (defun update-inits (class inits)
  473.   (setf (plist-value class 'default-initargs) inits))
  474.  
  475.  
  476. ;;;
  477. ;;;
  478. ;;;
  479. (defmethod compute-default-initargs ((class std-class) cpl direct)
  480.   (labels ((walk (tail)
  481.          (if (null tail)
  482.          nil
  483.          (let ((c (pop tail)))
  484.            (append (if (eq c class)
  485.                    direct 
  486.                    (class-direct-default-initargs c))
  487.                (walk tail))))))
  488.     (let ((initargs (walk cpl)))
  489.       (delete-duplicates initargs :test #'eq :key #'car :from-end t))))
  490.  
  491.  
  492. ;;;
  493. ;;; Protocols for constructing direct and effective slot definitions.
  494. ;;;
  495. ;;; 
  496. ;;;
  497. ;;;
  498. (defmethod direct-slot-definition-class ((class std-class) initargs)
  499.   (declare (ignore initargs))
  500.   (find-class 'standard-direct-slot-definition))
  501.  
  502. (defun make-direct-slotd (class initargs)
  503.   (let ((initargs (list* :class class initargs)))
  504.     (apply #'make-instance (direct-slot-definition-class class initargs) initargs)))
  505.  
  506. ;;;
  507. ;;;
  508. ;;;
  509. (defmethod compute-slots ((class std-class) cpl class-direct-slots)
  510.   ;;
  511.   ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
  512.   ;; for each different slot name we find in our superclasses.  Each
  513.   ;; call receives the class and a list of the dslotds with that name.
  514.   ;; The list is in most-specific-first order.
  515.   ;;
  516.   (let ((name-dslotds-alist ()))
  517.     (labels ((collect-one-class (dslotds)
  518.            (dolist (d dslotds)
  519.          (let* ((name (slotd-name d))
  520.             (entry (assq name name-dslotds-alist)))
  521.            (if entry
  522.                (push d (cdr entry))
  523.                (push (list name d) name-dslotds-alist))))))
  524.       (collect-one-class class-direct-slots)
  525.       (dolist (c (cdr cpl)) (collect-one-class (class-direct-slots c)))
  526.       (mapcar #'(lambda (direct)
  527.           (compute-effective-slot-definition class
  528.                              (nreverse (cdr direct))))
  529.           name-dslotds-alist))))
  530.  
  531. (defmethod compute-effective-slot-definition ((class std-class) dslotds)
  532.   (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
  533.      (class (effective-slot-definition-class class initargs)))
  534.     (apply #'make-instance class initargs)))
  535.  
  536. (defmethod effective-slot-definition-class ((class std-class) initargs)
  537.   (declare (ignore initargs))
  538.   (find-class 'standard-effective-slot-definition))
  539.  
  540. (defmethod compute-effective-slot-definition-initargs
  541.        ((class std-class) direct-slotds)
  542.   (let* ((name nil)
  543.      (initfunction nil)
  544.      (initform nil)
  545.      (initargs nil)
  546.      (allocation nil)
  547.      (type t)
  548.      (namep  nil)
  549.      (initp  nil)
  550.      (allocp nil))
  551.  
  552.     (dolist (slotd direct-slotds)
  553.       (when slotd
  554.     (unless namep
  555.       (setq name (slotd-name slotd)
  556.         namep t))
  557.     (unless initp
  558.       (when (slotd-initfunction slotd)
  559.         (setq initform (slotd-initform slotd)
  560.           initfunction (slotd-initfunction slotd)
  561.           initp t)))
  562.     (unless allocp
  563.       (setq allocation (slotd-allocation slotd)
  564.         allocp t))
  565.     (setq initargs (append (slotd-initargs slotd) initargs))
  566.     (let ((slotd-type (slotd-type slotd)))
  567.       (setq type (cond ((null type)     slotd-type)
  568.                ((subtypep type slotd-type) type)
  569.                (t `(and ,type ,slotd-type)))))))
  570.     (list :name name
  571.       :initform initform
  572.       :initfunction initfunction
  573.       :initargs initargs
  574.       :allocation allocation
  575.       :type type)))
  576.  
  577.  
  578. ;;;
  579. ;;; NOTE: For bootstrapping considerations, these can't use make-instance
  580. ;;;       to make the method object.  They have to use make-a-method which
  581. ;;;       is a specially bootstrapped mechanism for making standard methods.
  582. ;;;
  583. (defmethod add-reader-method ((class std-class) generic-function slot-name)
  584.   (let* ((name (class-name class))
  585.      (method (make-a-method 'standard-reader-method
  586.                 ()
  587.                 (list (or name 'standard-object))
  588.                 (list class)
  589.                 (make-reader-method-function class slot-name)
  590.                 "automatically generated reader method"
  591.                 slot-name)))
  592.     (add-method generic-function method)))
  593.  
  594. (defmethod add-writer-method ((class std-class) generic-function slot-name)
  595.   (let* ((name (class-name class))
  596.      (method (make-a-method 'standard-writer-method
  597.                 ()
  598.                 (list 'new-value (or name 'standard-object))
  599.                 (list *the-class-t* class)
  600.                 (make-writer-method-function class slot-name)
  601.                 "automatically generated writer method"
  602.                 slot-name)))
  603.     (add-method generic-function method)))
  604.  
  605.  
  606. (defmethod remove-reader-method ((class std-class) generic-function)
  607.   (let ((method (get-method generic-function () (list class) nil)))
  608.     (when method (remove-method generic-function method))))
  609.  
  610. (defmethod remove-writer-method ((class std-class) generic-function)
  611.   (let ((method
  612.       (get-method generic-function () (list *the-class-t* class) nil)))
  613.     (when method (remove-method generic-function method))))
  614.  
  615.  
  616. ;;;
  617. ;;; make-reader-method-function and make-write-method function are NOT part of
  618. ;;; the standard protocol.  They are however useful, PCL makes uses makes use
  619. ;;; of them internally and documents them for PCL users.
  620. ;;;
  621. ;;; *** This needs work to make type testing by the writer functions which
  622. ;;; *** do type testing faster.  The idea would be to have one constructor
  623. ;;; *** for each possible type test.  In order to do this it would be nice
  624. ;;; *** to have help from inform-type-system-about-class and friends.
  625. ;;;
  626. ;;; *** There is a subtle bug here which is going to have to be fixed.
  627. ;;; *** Namely, the simplistic use of the template has to be fixed.  We
  628. ;;; *** have to give the optimize-slot-value method the user might have
  629. ;;; *** defined for this metclass a chance to run.
  630. ;;;
  631. (defmethod make-reader-method-function ((class standard-class) slot-name)
  632.   (make-std-reader-method-function slot-name))
  633.  
  634. (defmethod make-writer-method-function ((class standard-class) slot-name)
  635.   (make-std-writer-method-function slot-name))
  636.  
  637. (defun make-std-reader-method-function (slot-name)
  638.   #'(lambda (instance)
  639.       (slot-value instance slot-name)))
  640.  
  641. (defun make-std-writer-method-function (slot-name)
  642.   #'(lambda (nv instance)
  643.       (setf (slot-value instance slot-name) nv)))
  644.   
  645.  
  646.  
  647. ;;;; inform-type-system-about-class
  648. ;;;; make-type-predicate
  649. ;;;
  650. ;;; These are NOT part of the standard protocol.  They are internal mechanism
  651. ;;; which PCL uses to *try* and tell the type system about class definitions.
  652. ;;; In a more fully integrated implementation of CLOS, the type system would
  653. ;;; know about class objects and class names in a more fundamental way and
  654. ;;; the mechanism used to inform the type system about new classes would be
  655. ;;; different.
  656. ;;;
  657. (defmethod inform-type-system-about-class ((class std-class) name)
  658.   (let ((predicate-name (make-type-predicate-name name)))
  659.     (setf (symbol-function predicate-name) (make-type-predicate name))
  660.     (do-satisfies-deftype name predicate-name)))
  661.  
  662. (defun make-type-predicate (name)
  663.   #'(lambda (x)
  664.       (not
  665.     (null
  666.       (memq (find-class name)
  667.         (cond ((std-instance-p x)
  668.                (class-precedence-list (std-instance-class x)))
  669.               ((fsc-instance-p x)
  670.                (class-precedence-list (fsc-instance-class x)))))))))
  671.  
  672.  
  673. ;;;
  674. ;;; These 4 definitions appear here for bootstrapping reasons.  Logically,
  675. ;;; they should be in the construct file.  For documentation purposes, a
  676. ;;; copy of these definitions appears in the construct file.  If you change
  677. ;;; one of the definitions here, be sure to change the copy there.
  678. ;;; 
  679. (defvar *initialization-generic-functions*
  680.     (list #'make-instance
  681.           #'default-initargs
  682.           #'allocate-instance
  683.           #'initialize-instance
  684.           #'shared-initialize))
  685.  
  686. (defmethod maybe-update-constructors
  687.        ((generic-function generic-function)
  688.         (method method))
  689.   (when (memq generic-function *initialization-generic-functions*)
  690.     (labels ((recurse (class)
  691.            (update-constructors class)
  692.            (dolist (subclass (class-direct-subclasses class))
  693.          (recurse subclass))))
  694.       (when (classp (car (method-specializers method)))
  695.     (recurse (car (method-specializers method)))))))
  696.  
  697. (defmethod update-constructors ((class std-class))
  698.   (dolist (cons (class-constructors class))
  699.     (install-lazy-constructor-installer cons)))
  700.  
  701. (defmethod update-constructors ((class class))
  702.   ())
  703.  
  704.  
  705.  
  706. (defmethod compatible-meta-class-change-p (class proto-new-class)
  707.   (eq (class-of class) (class-of proto-new-class)))
  708.  
  709. (defmethod check-super-metaclass-compatibility ((class t) (new-super t))
  710.   (unless (eq (class-of class) (class-of new-super))
  711.     (error "The class ~S was specified as a~%super-class of the class ~S;~%~
  712.             but the meta-classes ~S and~%~S are incompatible."
  713.        new-super class (class-of new-super) (class-of class))))
  714.  
  715.  
  716. ;;;
  717. ;;;
  718. ;;;
  719. (defun force-cache-flushes (class)
  720.   (let* ((owrapper (class-wrapper class))
  721.      (state (wrapper-state owrapper)))
  722.     ;;
  723.     ;; We only need to do something if the state is still T.  If the
  724.     ;; state isn't T, it will be FLUSH or OBSOLETE, and both of those
  725.     ;; will already be doing what we want.  In particular, we must be
  726.     ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
  727.     ;; means do what FLUSH does and then some.
  728.     ;; 
  729.     (when (eq state 't)
  730.       (let ((nwrapper (make-wrapper class)))
  731.     (setf (wrapper-instance-slots-layout nwrapper)
  732.           (wrapper-instance-slots-layout owrapper))
  733.     (setf (wrapper-class-slots nwrapper)
  734.           (wrapper-class-slots owrapper))
  735.     (without-interrupts
  736.       (setf (slot-value class 'wrapper) nwrapper)
  737.       (invalidate-wrapper owrapper 'flush nwrapper))
  738.     (update-constructors class)))))        ;??? ***
  739.  
  740. (defun flush-cache-trap (owrapper nwrapper instance)
  741.   (declare (ignore owrapper))
  742.   (set-wrapper instance nwrapper))
  743.  
  744.  
  745.  
  746. ;;;
  747. ;;; make-instances-obsolete can be called by user code.  It will cause the
  748. ;;; next access to the instance (as defined in 88-002R) to trap through the
  749. ;;; update-instance-for-redefined-class mechanism.
  750. ;;; 
  751. (defmethod make-instances-obsolete ((class std-class))
  752.   (let ((owrapper (class-wrapper class))
  753.     (nwrapper (make-wrapper class)))
  754.       (setf (wrapper-instance-slots-layout nwrapper)
  755.         (wrapper-instance-slots-layout owrapper))
  756.       (setf (wrapper-class-slots nwrapper)
  757.         (wrapper-class-slots owrapper))
  758.       (without-interrupts
  759.     (setf (slot-value class 'wrapper) nwrapper)
  760.     (invalidate-wrapper owrapper 'obsolete nwrapper)
  761.     class)))
  762.  
  763. (defmethod make-instances-obsolete ((class symbol))
  764.   (make-instances-obsolete (find-class class)))
  765.  
  766.  
  767. ;;;
  768. ;;; obsolete-instance-trap is the internal trap that is called when we see
  769. ;;; an obsolete instance.  The times when it is called are:
  770. ;;;   - when the instance is involved in method lookup
  771. ;;;   - when attempting to access a slot of an instance
  772. ;;;
  773. ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
  774. ;;; access macros.
  775. ;;;
  776. ;;; Of course these times when it is called are an internal implementation
  777. ;;; detail of PCL and are not part of the documented description of when the
  778. ;;; obsolete instance update happens.  The documented description is as it
  779. ;;; appears in 88-002R.
  780. ;;;
  781. ;;; This has to return the new wrapper, so it counts on all the methods on
  782. ;;; obsolete-instance-trap-internal to return the new wrapper.  It also does
  783. ;;; a little internal error checking to make sure that the traps are only
  784. ;;; happening when they should, and that the trap methods are computing
  785. ;;; apropriate new wrappers.
  786. ;;; 
  787. (defun obsolete-instance-trap (owrapper nwrapper instance)  
  788.   ;;
  789.   ;; local  --> local        transfer 
  790.   ;; local  --> shared       discard
  791.   ;; local  -->  --          discard
  792.   ;; shared --> local        transfer
  793.   ;; shared --> shared       discard
  794.   ;; shared -->  --          discard
  795.   ;;  --    --> local        add
  796.   ;;  --    --> shared        --
  797.   ;;
  798.   (let* ((class (wrapper-class nwrapper))
  799.      (guts (allocate-instance class))    ;??? allocate-instance ???
  800.      (olayout (wrapper-instance-slots-layout owrapper))
  801.      (nlayout (wrapper-instance-slots-layout nwrapper))
  802.      (oslots (get-slots instance))
  803.      (nslots (get-slots guts))
  804.      (oclass-slots (wrapper-class-slots owrapper))
  805.      (added ())
  806.      (discarded ())
  807.      (plist ()))
  808.     ;;
  809.     ;; Go through all the old local slots.
  810.     ;; 
  811.     (iterate ((name (list-elements olayout))
  812.           (opos (interval :from 0)))
  813.       (let ((npos (posq name nlayout)))
  814.     (if npos
  815.         (setf (svref nslots npos) (svref oslots opos))
  816.         (progn (push name discarded)
  817.            (unless (eq (svref oslots opos) *slot-unbound*)
  818.              (setf (getf plist name) (svref oslots opos)))))))
  819.     ;;
  820.     ;; Go through all the old shared slots.
  821.     ;;
  822.     (iterate ((oclass-slot-and-val (list-elements oclass-slots)))
  823.       (let ((name (car oclass-slot-and-val))
  824.         (val (cdr oclass-slot-and-val)))
  825.     (let ((npos (posq name nlayout)))
  826.       (if npos
  827.           (setf (svref nslots npos) (cdr oclass-slot-and-val))
  828.           (progn (push name discarded)
  829.              (unless (eq val *slot-unbound*)
  830.                (setf (getf plist name) val)))))))
  831.     ;;
  832.     ;; Go through all the new local slots to compute the added slots.
  833.     ;; 
  834.     (dolist (nlocal nlayout)
  835.       (unless (or (memq nlocal olayout)
  836.           (assq nlocal oclass-slots))
  837.     (push nlocal added)))
  838.       
  839.     (without-interrupts
  840.       (set-wrapper instance nwrapper)
  841.       (set-slots instance nslots))
  842.  
  843.     (update-instance-for-redefined-class instance
  844.                      added
  845.                      discarded
  846.                      plist)
  847.     nwrapper))
  848.  
  849.  
  850.  
  851. ;;;
  852. ;;;
  853. ;;;
  854. (defmacro change-class-internal (wrapper-fetcher slots-fetcher alloc)
  855.   `(let* ((old-class (class-of instance))
  856.       (copy (,alloc old-class))
  857.       (guts (,alloc new-class))
  858.       (new-wrapper (,wrapper-fetcher guts))
  859.       (old-wrapper (class-wrapper old-class))
  860.       (old-layout (wrapper-instance-slots-layout old-wrapper))
  861.       (new-layout (wrapper-instance-slots-layout new-wrapper))
  862.       (old-slots (,slots-fetcher instance))
  863.       (new-slots (,slots-fetcher guts))
  864.       (old-class-slots (wrapper-class-slots old-wrapper)))
  865.  
  866.     ;;
  867.     ;; "The values of local slots specified by both the class Cto and
  868.     ;; Cfrom are retained.  If such a local slot was unbound, it remains
  869.     ;; unbound."
  870.     ;;     
  871.     (iterate ((new-slot (list-elements new-layout))
  872.           (new-position (interval :from 0)))
  873.       (let ((old-position (position new-slot old-layout :test #'eq)))
  874.     (when old-position
  875.       (setf (svref new-slots new-position)
  876.         (svref old-slots old-position)))))
  877.  
  878.     ;;
  879.     ;; "The values of slots specified as shared in the class Cfrom and
  880.     ;; as local in the class Cto are retained."
  881.     ;;
  882.     (iterate ((slot-and-val (list-elements old-class-slots)))
  883.       (let ((position (position (car slot-and-val) new-layout :test #'eq)))
  884.     (when position
  885.       (setf (svref new-slots position) (cdr slot-and-val)))))
  886.  
  887.     ;; Make the copy point to the old instance's storage, and make the
  888.     ;; old instance point to the new storage.
  889.     (without-interrupts
  890.       (setf (,slots-fetcher copy) old-slots)
  891.       
  892.       (setf (,wrapper-fetcher instance) new-wrapper)
  893.       (setf (,slots-fetcher instance) new-slots))
  894.  
  895.     (update-instance-for-different-class copy instance)
  896.     instance))
  897.  
  898. (defmethod change-class ((instance standard-object)
  899.              (new-class standard-class))
  900.   (unless (std-instance-p instance)
  901.     (error "Can't change the class of ~S to ~S~@
  902.             because it isn't already an instance with metaclass~%~S."
  903.        instance
  904.        new-class
  905.        'standard-class))
  906.   (change-class-internal std-instance-wrapper
  907.              std-instance-slots
  908.              allocate-instance))
  909.  
  910. (defmethod change-class ((instance standard-object)
  911.              (new-class funcallable-standard-class))
  912.   (unless (fsc-instance-p instance)
  913.     (error "Can't change the class of ~S to ~S~@
  914.             because it isn't already an instance with metaclass~%~S."
  915.        instance
  916.        new-class
  917.        'funcallable-standard-class))
  918.   (change-class-internal fsc-instance-wrapper
  919.              fsc-instance-slots
  920.              allocate-instance))
  921.  
  922. (defmethod change-class ((instance t) (new-class-name symbol))
  923.   (change-class instance (find-class new-class-name)))
  924.  
  925.  
  926.  
  927. ;;;
  928. ;;; The metaclass BUILT-IN-CLASS
  929. ;;;
  930. ;;; This metaclass is something of a weird creature.  By this point, all
  931. ;;; instances of it which will exist have been created, and no instance
  932. ;;; is ever created by calling MAKE-INSTANCE.
  933. ;;;
  934. ;;; But, there are other parts of the protcol we must follow and those
  935. ;;; definitions appear here.
  936. ;;; 
  937. (defmethod shared-initialize :before
  938.        ((class built-in-class) slot-names &rest initargs)
  939.   (declare (ignore slot-names))
  940.   (error "Attempt to initialize or reinitialize a built in class."))
  941.  
  942. (defmethod class-direct-slots            ((class built-in-class)) ())
  943. (defmethod class-slots                   ((class built-in-class)) ())
  944. (defmethod class-direct-default-initargs ((class built-in-class)) ())
  945. (defmethod class-default-initargs        ((class built-in-class)) ())
  946.  
  947. (defmethod check-super-metaclass-compatibility ((c class) (s built-in-class))
  948.   (or (eq s *the-class-t*)
  949.       (error "~S cannot have ~S as a super.~%~
  950.               The class ~S is the only built in class that can be a~%~
  951.               superclass of a standard class."
  952.          c s *the-class-t*)))
  953.  
  954.  
  955. ;;;
  956. ;;;
  957. ;;;
  958.  
  959. (defmethod check-super-metaclass-compatibility ((c std-class)
  960.                         (f forward-referenced-class))
  961.   't)
  962.  
  963.  
  964. ;;;
  965. ;;;
  966. ;;;
  967.  
  968. (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
  969.   (pushnew dependent (plist-value metaobject 'dependents)))
  970.  
  971. (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
  972.   (setf (plist-value metaobject 'dependents)
  973.     (delete dependent (plist-value metaobject 'dependents))))
  974.  
  975. (defmethod map-dependents ((metaobject dependent-update-mixin) function)
  976.   (dolist (dependent (plist-value metaobject 'dependents))
  977.     (funcall function dependent)))
  978.